//
//  SetNameVC.swift
//  NewXueBa
//
//  Created by Shine on 2019/6/12.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import UIKit

class SetNameVC: BCBaseVC {

    @IBOutlet weak var vcTitle: UILabel!
    @IBOutlet weak var enterBtn: UIButton!
    @IBOutlet weak var nameTextF: UITextField!
    
    private let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.fd_prefersNavigationBarHidden = true //隐藏导航栏
        nameTextF.text = BCUserManager.shareManager.user_name
        nameTextObserver()
    }

    @IBAction func backClick(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func enterBtnClcik(_ sender: UIButton) {
        updateNicknameRequest()
    }
    
    fileprivate func nameTextObserver() {
        nameTextF.rx.text.asObservable()
            .subscribe(onNext: {  [weak self] (inputText) in
                if let _ = inputText {
                    self?.enterBtn.isUserInteractionEnabled = true
                    self?.enterBtn.setTitleColor(UIColor.hex(hexString: "#2E3D4B"), for: .normal)
                } else {
                    self?.enterBtn.isUserInteractionEnabled = false
                    self?.enterBtn.setTitleColor(UIColor.hex(hexString: "#7F899A"), for: .normal)
                }
            }).disposed(by: disposeBag)
    }
    
    fileprivate func updateNicknameRequest() {
        let token = BCAPI.update_nickname(nickName: nameTextF.text ?? "")
        ApiLoadingProvider.request(token, model: BaseModel.self) { [weak self] (respons) in
            if let code = respons?.code, code == 0 {
                BCUserManager.shareManager.user_name = self?.nameTextF.text
                self?.navigationController?.popViewController(animated: true)
                BCBaseVC.showTipMsgInWindow("修改完成", time: 2)
            }
        }
    }
}
